package org.budo.support.servlet.util;

import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author limingwei
 * @date 2016年6月7日 上午11:36:03
 */
public class RequestUtil {
    public static void forward(String path, HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher(path).forward(request, response);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public static String getQueryString(HttpServletRequest request) {
        String queryString = "";
        Boolean first = true;
        Set<Entry<String, String[]>> set = request.getParameterMap().entrySet();
        for (Entry<String, String[]> entry : set) {
            String key = entry.getKey();
            String[] values = entry.getValue();
            for (String value : values) {
                if (!first) {
                    queryString += "&";
                } else {
                    first = false;
                }
                queryString += key + "=" + value;
            }
        }
        return queryString;
    }
}